package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 动态展示菜单
 * 根据当前登录用户查询 t_menu 表中的数据, 动态展示菜单数据
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;


    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer pageNum = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(pageNum,pageSize);

        Page<Menu> page = menuDao.findBycondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Map<String,String> map) {
        String level = map.get("level");

        //获取菜单排序号
        String path = map.get("path");
        char c = path.charAt(path.length() - 1);
        map.put("priority", String.valueOf(c));

        if (level != null) {//添加的是一级菜单
             menuDao.add(map);
        }
    }

    @Override
    public Map<String, Object> findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Map<String, String> map) {
        String id = map.get("id");//获取要修改的id

        //获取priority排序值
        String path = map.get("path");
        char c = path.charAt(path.length() - 1);
        map.put("priority", String.valueOf(c));

        //首先查询此id的菜单信息
        Map<String, Object> objectMap = menuDao.findById(Integer.parseInt(id));
        Integer level = (Integer) objectMap.get("level");

        if (level == 1) {//要被修改的是一级菜单

            Integer count = menuDao.getCountParentByid(Integer.parseInt(id));//查询此一级菜单是否有二级菜单
            if (count > 0 && Integer.parseInt(map.get("level")) == 2) { //想要更换为二级菜单
                throw new RuntimeException();
            }
        }
        menuDao.edit(map);
    }

	@Override
	public List<Menu> findMenuByUsername(String username) {
		//根据用户名查询t_menu表
		List<Menu> parentMenList = menuDao.findMenuByUsername(username);
		for (Menu menu : parentMenList) {
			Integer id = menu.getId();
			List<Menu> childMenuList = menuDao.findChildMenuListByParentMenuIdAndRoleName(id, username);
			menu.setChildren(childMenuList);
		}
		return parentMenList;
	}

    @Override
    public void delete(Integer id) {
        Integer roleid = menuDao.findByMenuId(id);
        if(roleid != null) {
            menuDao.deleteByMenuId(id);
        }
        menuDao.delete(id);


    }

    @Override
    public List<Menu> findParentmenu() {
        List<Menu> parentmenu = menuDao.findParentmenu();
        List<Menu> list = new ArrayList<>();
        for (Menu menu : parentmenu) {
            if(menu.getParentMenuId()== null){
                list.add(menu);
            }
        }
        return list;

    }


}
